<template>
    <div>
        <div class="bg-banner" />
        <div id="login-box">
            <div class="login-banner" />
            <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-form" auto-complete="on" label-position="left">
                <div class="title-container">
                    <h3 class="title">{{ title }}管理系统</h3>
                </div>
                <div>
                    <!-- 用户名 -->
                    <el-form-item prop="username">
                        <el-input ref="name" v-model="loginForm.username" placeholder="请输入用户名" type="text" tabindex="1" auto-complete="on" @keyup.enter.native="handleLogin">
                            <svg-icon slot="prefix" name="user" />
                        </el-input>
                    </el-form-item>
                    <!-- 密码 -->
                    <el-form-item prop="password">
                        <el-input ref="password" v-model="loginForm.password" :type="passwordType" placeholder="请输入密码" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin">
                            <svg-icon slot="prefix" name="password" />
                            <svg-icon slot="suffix" :name="passwordType === 'password' ? 'eye' : 'eye-open'" @click="showPassword" />
                        </el-input>
                    </el-form-item>
                    <!-- 验证码 -->
                    <el-form-item prop="code">
                        <el-input v-model="loginForm.code" type="text" placeholder="点击图片更换验证码" auto-complete="false" style="width: 290px; margin-right: 5px;" @keyup.enter.native="handleLogin" />
                        <img :src="captchaUrl" @click="updateCaptcha">
                    </el-form-item>
                </div>
                <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
                <el-button style="margin-left: 20px; border: none; font-size: 14px; background-color: #faf4f4;" @click="goTo()">忘记密码</el-button>
                <el-button :loading="loading" type="primary" style="width: 100%;" @click.native.prevent="handleLogin">登 录</el-button>
                <div style="margin-top: 20px; margin-bottom: -10px; color: #666; font-size: 14px; text-align: center; font-weight: bold;">
                    <el-button type="danger" size="mini" @click="testAccount('admin')">admin</el-button>
                    <el-button type="danger" size="mini" plain @click="dialogFormVisible = true">快速注册</el-button>
                </div>
            </el-form>
        </div>
        <Copyright v-if="$store.state.settings.showCopyright" />
        <!-- 注册弹框 -->
        <el-dialog title="快速注册" width="30%" :visible.sync="dialogFormVisible">
            <div class="dialog_body">
                <el-form :model="loginForm">
                    <el-form-item prop="phone">
                        <el-input ref="phone" v-model="loginForm.phone" placeholder="请输入注册手机号/邮箱" type="text" tabindex="1" auto-complete="on" />
                    </el-form-item>
                    <el-form-item prop="verify">
                        <el-input ref="verify" v-model="loginForm.verify" placeholder="请输入短信验证码" tabindex="2" auto-complete="on" />
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input ref="password" v-model="loginForm.password" placeholder="8-20位，包含字符数字至少两种" type="text" tabindex="1" auto-complete="on" />
                    </el-form-item>
                    <el-button type="primary" style="font-size: 18px; width: 100%;">立即注册</el-button>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <button @click="dialogFormVisible = false">返回账号登录</button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { Message } from 'element-ui'
import store from '@/store/index'
import {selectUserInfo, getMenus} from '@/api/index'
export default {
    name: 'Login',
    data() {
        return {
            captchaUrl: '/common/captcha?time=' + new Date(),
            title: process.env.VUE_APP_TITLE,
            dialogFormVisible: false,
            loginForm: {
                username: localStorage.login_account || '', // 创建一个本地存储的name/value对,保存网站数据
                password: '', // 先置空
                remember: !!localStorage.login_account, // 记住密码
                code: ''
            },
            rules: { // 校验,与prop名对应
                username: [
                    { required: true, trigger: 'blur', message: '请输入用户名' } // 没输入时会弹出提醒
                ],
                password: [
                    { required: true, trigger: 'blur', message: '请输入密码' },
                    { min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' }
                ],
                code: [{required: true,  trigger: 'blur', message: '请输入验证码'}]
            },
            loading: false, // 判断登录
            passwordType: 'password', // 不可见的密码输入框类型
            redirect: undefined
        }
    },
    watch: { // 监听属性变化
        $route: {
            handler: function(route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    methods: {
        updateCaptcha() {
            this.captchaUrl = '/common/captcha?time=' + new Date()

        },
        showPassword() {
            this.passwordType = this.passwordType === 'password' ? '' : 'password' // 置空可见，点一下
            this.$nextTick(() => {  // 等页面渲染完成，
                this.$refs.password.focus() // 鼠标的焦点在密码输入框上面
            })
        },
        goTo() {
            this.$router.push({path: '/forgot' })
        },
        handleLogin: function() { // 在密码处和登录框点击回车，处理登录事件
            this.$refs.loginForm.validate(valid => {
                if (valid) {
                    this.postRequest('/login', this.loginForm).then(resp => {
                        if (resp.data.code === 200 && resp.data.message === '登录成功') {
                            Message.success({message: resp.data.message})
                            var userInfo = {token: resp.data.obj.tokenHead + ' ' + resp.data.obj.token}
                            // 查询用户信息
                            selectUserInfo(userInfo.token).then(resp => {
                                userInfo.account = resp.data
                                var roleKey = userInfo.account.roles[0].roleKey
                                if (roleKey !== '' && (roleKey === 'ROLE_ADMIN' || roleKey === 'ROLE_SUPER')) {
                                    userInfo.permissions = [{isAdmin: true}]
                                } else {
                                    userInfo.permissions = [{isAdmin: false}]
                                }
                                getMenus(userInfo.token).then(resp => {
                                    // 获取菜单信息 暂时写死 TODO
                                    // 查询用户信息
                                    // userInfo.permissions = [{isAdmin: true}]
                                    // 将用户信息 存入store
                                    store.dispatch('user/setUserInfo', userInfo).then(resp => {
                                        this.$router.push('/dashboard')
                                    })
                                })

                            })
                        }
                    })
                } else {
                    this.$message.error('请输入所有字段！')
                    return false
                }
            })
        },
        testAccount(username) {
            this.loginForm.username = username
            this.loginForm.password = '123456'
            this.handleLogin()
        }
    }
}
</script>

<style lang="scss" scoped>
[data-mode=mobile] {
    #login-box {
        max-width: 80%;
        .login-banner {
            display: none;
        }
    }
}
::v-deep input[type=password]::-ms-reveal {
    display: none;
}
.bg-banner {
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    background-image: url(../assets/images/login-bg2.jpg);
    opacity: 0.2;
    background-size: cover;
    background-position: center center;
}
#login-box {
    display: flex;
    justify-content: space-between;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background: rgba(255, 255, 255, 0.8);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 0 5px #999;
    .login-banner {
        width: 300px;
        background-image: url(../assets/images/login-bg3.jpg);
        background-size: cover;
        background-position: center center;
        opacity: 0.9;
    }
    .login-form {
        width: 450px;
        padding: 50px 35px 30px;
        overflow: hidden;
        background-color: #faf4f4;
        .svg-icon {
            vertical-align: -0.35em;
        }
        .title-container {
            position: relative;
            .title {
                font-size: 22px;
                color: #666;
                margin: 0 auto 30px;
                text-align: center;
                font-weight: bold;
                @include text-overflow;
            }
        }
    }
    ::v-deep .el-input {
        display: inline-block;
        height: 48px;
        width: 100%;
        input {
            height: 48px;
        }
        .el-input__prefix {
            left: 10px;
        }
        .el-input__suffix {
            right: 10px;
        }
    }
    .el-checkbox {
        margin: 20px 0;
    }
}
.copyright {
    position: absolute;
    bottom: 30px;
    width: 100%;
    margin: 0;
    mix-blend-mode: difference;
}
.el-form-item {
    ::v-deep .el-form-item__content {
        display: flex;
        align-items: center;
    }
}
.dialog-footer {
    button {
        font-size: 14px;
        color: #409eff;
        border: none;
        background-color: white;
        margin: 0 auto;
        display: block;
    }
}
</style>
